/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Fri 07 Sep 2018 06:59:43 PM CST
 * Destription: 
 *   测试set集合修改比较函数是否调用的问题
 */

#include <iostream>
#include <string>
#include <stdio.h>
#include <set>

using namespace std;

typedef struct IpInfo_t
{
    std::string key_;       //hash的key
    std::string ip_;        //ip地址
    std::string port_;      //端口号
    unsigned int priority_; //优先级
    int type_;           //ip类型
    /*
    bool operator<(const IpInfo_t&right) const
    {
        if(this->type_ < right.type_) {
            printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, this->type_, right.type_);
            return true;
        }
        else if(this->type_ > right.type_) {
            printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, this->type_, right.type_);
            return false;
        }
        else if(this->priority_ < right.priority_) {
            printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, this->priority_, right.priority_);
            return true;
        }
        else if(this->priority_ > right.priority_) {
            printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, this->priority_, right.priority_);
            return false;
        }
        else {
            printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, this->priority_, right.priority_);
            return this->ip_ < right.ip_;
        }
    }
    */
} IpInfo_t;

namespace std
{
    template<>
    struct less<IpInfo_t>
    {
        bool operator()(const IpInfo_t& left, const IpInfo_t& right) const
        {
            if(left.type_ < right.type_) {
                printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, left.type_, right.type_);
                return true;
            }
            else if(left.type_ > right.type_) {
                printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, left.type_, right.type_);
                return false;
            }
            else if(left.priority_ < right.priority_) {
                printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, left.priority_, right.priority_);
                return true;
            }
            else if(left.priority_ > right.priority_) {
                printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, left.priority_, right.priority_);
                return false;
            }
            else {
                printf("%s:%d, tl=%d, tr=%d\n",__func__,__LINE__, left.priority_, right.priority_);
                return left.ip_ < right.ip_;
            }
        }
    };
}

void test_set()
{
    std::set<IpInfo_t> tmpSet;
    for(int i=0; i<10; ++i) {
        IpInfo_t tmpip;
        char ip[64] = {0};
        sprintf(ip, "10.1.1.%d", i);
        tmpip.ip_= ip;
        tmpip.type_ = i;
        tmpip.priority_ = i;
        tmpip.key_ = ip;
        tmpip.port_ = "80";
        tmpSet.insert(tmpip);
        printf("insert %d\n", i);
    }
}

int main(int argc, char *argv[])
{
    test_set();    
    return 0;
}
